﻿using ScanSnap.Util;
using System.Windows;
using System.Windows.Controls;
using System.Configuration;
using System.Collections.Generic;
using ScanSnap.Wpf.WpfWindow;

namespace ScanSnap.Wpf.WpfPage
{
    /// <summary>
    /// Interaction logic for SettingsPage.xaml
    /// </summary>
    public partial class SettingsPage : Page
    {
        private static SettingsPage instance;
        public IList<string> ErrorList {get; set;}

        private SettingsPage()
        {
            InitializeComponent();
            ErrorList = new List<string>();
            SetDefaultValues();
        }

        private SettingsPage(IList<string> errorList)
        {
            InitializeComponent();
            ErrorList = errorList;
            ErrorLabel.Content = ValidateUtility.ListToString(ErrorList);
            SetDefaultValues();
        }

        public static SettingsPage Instance
        {
            get
            {
                if (instance == null)
                {
                    instance = new SettingsPage();
                }

                instance.ValidateValues();

                return instance;
            }
        }

        //public static SettingsPage Instance(IList<string> errorList)
        //{
        //    if (instance.ErrorList.Equals(errorList))
        //    {
        //        instance = new SettingsPage(errorList);
        //    }

        //    return instance;
        //}

        private void SetDefaultValues()
        {
            textBoxScanningFolder.Text = ApplicationSettings.SCAN_FOLDER;
            textBoxServerUrl.Text = ApplicationSettings.SERVER_URL;

            ValidateValues();
        }

        private void ValidateValues()
        {
            string url = textBoxServerUrl.Text;
            string folderPath = textBoxScanningFolder.Text;
            ErrorList = ApplicationSettings.UpdateValues(url, folderPath);

            if (ErrorList.Count > 0)
            {
                ErrorLabel.Style = (Style)FindResource("ErrorLabel");
                ErrorLabel.Content = ValidateUtility.ListToString(ErrorList);
            }
            else
            {
                ErrorLabel.Style = (Style)FindResource("MessageLabel");
                ErrorLabel.Content = "Server Registerd";

            }
        }

        private void ButtonSave_Click(object sender, RoutedEventArgs e)
        {
            string url = textBoxServerUrl.Text;
            string folderPath = textBoxScanningFolder.Text;
            ErrorList = ApplicationSettings.UpdateValues(url, folderPath);

            if (ErrorList.Count > 0)
            {
                ErrorLabel.Style = (Style)FindResource("ErrorLabel");
                ErrorLabel.Content = ValidateUtility.ListToString(ErrorList);
            }
            else
            {
                ErrorLabel.Style = (Style)FindResource("MessageLabel");
                ErrorLabel.Content = "Successfully Updated The Server";

            }
            
        }

        private void ScanFolderBrowseButton_Click(object sender, RoutedEventArgs e)
        {
            System.Windows.Forms.FolderBrowserDialog folderDialog = new System.Windows.Forms.FolderBrowserDialog();
            
            if (textBoxScanningFolder.Text != null || textBoxScanningFolder.Text.Trim().Length != 0)
                folderDialog.SelectedPath = textBoxScanningFolder.Text;
            else
                folderDialog.SelectedPath = "C:\\";
            

            System.Windows.Forms.DialogResult result = folderDialog.ShowDialog();
            if (result == System.Windows.Forms.DialogResult.OK)
                textBoxScanningFolder.Text = folderDialog.SelectedPath;
        }


        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            //MainWindow.Instance.TransferToSignInPage();
        }
    }
}
